package com.zhihaihe.file.config;


import com.zhihaihe.file.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author web mvc 终端控制器
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
	@Autowired
	private AuthenticationInterceptor authenticationInterceptor;

	/**
	 * 接口拦截
	 * app拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		List<String> list = new ArrayList<>();
		list.add("/file/**");
		list.add("/filetransfer/**");
		list.add("/recoveryfile/**");
		list.add("/share/sharefile");
		list.add("/share/savesharefile");
		list.add("/share/shareList");
		list.add("/share/shareList");
//		list.add("/announcement/**");
		registry.addInterceptor(authenticationInterceptor)
			.addPathPatterns(list)
			.excludePathPatterns("/file",
					"/filetransfer/downloadfile",
					"/filetransfer/preview");
	}
	/**
	 * 这里主要为了解决跨域问题,所以重写addCorsMappings方法
	 */
	@Override
	protected void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins("*")
				.allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
				.allowedHeaders("*")
				.exposedHeaders("access-control-allow-headers",
						"access-control-allow-methods",
						"access-control-allow-origin",
						"access-control-max-age",
						"X-Frame-Options")
				.allowCredentials(false).maxAge(3600);
		super.addCorsMappings(registry);
	}

}